package cas.ihep.hep.test;

import cas.ihep.hep.analysis.TestDemo;
import cas.ihep.hep.analysis.*;
import cas.ihep.hep.util.MyApplication;

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.HashMap;

public class SparkMain {

    private static final HashMap<String,Class<? extends MyApplication>> appClass=new HashMap<>();

    static{
        appClass.put("LoopDemo", LoopDemo.class);
        appClass.put("RDDDemo", RDDDemo.class);
        appClass.put("RDDPWA", RDDPWA.class);
        appClass.put("NewPWA", NewLoopPWA.class);
        appClass.put("Test", TestDemo.class);
        appClass.put("DataInJVM", DataInJVM.class);
    }

    private static Class<?extends MyApplication> getApplication(String key) {
        Class<?extends MyApplication> ret=appClass.get(key);
        if(ret==null){
            try {
                Class<?> klass = Class.forName(key);
                ret=klass.asSubclass(MyApplication.class);
            }catch (ClassNotFoundException e){
                System.out.println(key+" is not a registered alias in SparkMain nor a valid java class name.\n" +
                        "Usage: SparkMain <alias name/java class> [args]\n"+
                        "Supported alias: NewPWA, RDDPWA, LoopDemo, RDDDemo, Test, DataInJVM");
                return null;
            }
        }
        return ret;
    }

    public static void main(String[] args) throws Exception {
        Class<? extends MyApplication> appKlass=getApplication(args[0]);
        if(appKlass==null){
            return;
        }
        Constructor<? extends MyApplication> init=appKlass.getConstructor(String[].class);
        MyApplication app=init.newInstance(new Object[]{Arrays.copyOfRange(args,1,args.length)});
        app.run();
    }
}
